import resources.RegistNetPropertyBundle;
import registnet.dao.*;

/* [start] 	initializes gui variables */
RegistNetPropertyBundle bundle = RegistNetPropertyBundle.getInstance("/resources/registnet");

String _logo =  bundle.getProperty("registnet.logo");

String _unit = bundle.getProperty("registnet.login.form.user.unit");

String _authors = bundle.getProperty("registnet.user.registrar.toolbar.authors");
String _help = bundle.getProperty("registnet.user.registrar.toolbar.help");

String _welcome = bundle.getProperty("registnet.user.registrar.welcome");
String _sign_out = bundle.getProperty("registnet.user.registrar.sign_out");
String _my_account = bundle.getProperty("registnet.user.registrar.my_account");
String _home = bundle.getProperty("registnet.user.registrar.home");
String _menu = bundle.getProperty("registnet.user.registrar.menu");
String _search = bundle.getProperty("registnet.user.registrar.search");

String _data_management = bundle.getProperty("registnet.user.registrar.menuitem.data_management");
String _courses = bundle.getProperty("registnet.user.registrar.menuitem.data_management.courses");
String _sections = bundle.getProperty("registnet.user.registrar.menuitem.data_management.sections");

String _grades = bundle.getProperty("registnet.user.registrar.menuitem.grades");
String _gradesheet = bundle.getProperty("registnet.user.registrar.menuitem.grades.gradesheet");

String _icon_home = bundle.getProperty("registnet.icons.home");
HashMap map_tab = new HashMap();
/* [end] */



/* [start]	defines an event listener;
			when a tab is closed, it is removed from the hashmap */
private class CloseTabEventListener implements EventListener {
	private String source;
	
	public CloseTabEventListener (String source){
		this.source = source;
	}
			
	public void onEvent(Event event) throws UiException {
		map_tab.remove(source);
	}
}
/* [end] */



/* [start]	retrieves session variables */
String _user = session.getAttribute("_username");
String _user_type = session.getAttribute("_user_type");
String _office = session.getAttribute("_office");
String _office_name = new UnitPeer().getUnit(_office).getUnitName();
/* [end] */



/* [start]	checks if session is valid */
boolean _isValidSession = false;
try{
	_isValidSession = session.getAttribute("_user_type").equals(_unit);
}catch(Exception e) { _isValidSession = false; }
/* [end] */



/* [start]	displays the grades module */
void showGrades(String object){
	if(!map_tab.containsKey(object)){		
		Tabs tabs = stage.getTabs();
		Tab tab = new Tab(object);
		tab.setClosable(true);
		tab.setParent(tabs);
		tab.addEventListener("onClose", new CloseTabEventListener(object));
		
		Tabpanels tabpanels = stage.getTabpanels();
		Tabpanel tabpanel = new Tabpanel();
		tabpanel.setSclass("background_light_blue");
		tabpanel.setParent(tabpanels);
		stage.setSelectedPanel(tabpanel);
		
		map_tab.put(object, tab);
		
		if(object.equals(_gradesheet)){
			Component win = Executions.createComponents("../../WEB-INF/macros/grades/gradesheet.zul", tabpanel, null);
		}
		if(object.equals("Grade Submission")){
			Component win = Executions.createComponents("../../WEB-INF/macros/grades/grade_submission.zul", tabpanel, null);
		}
	}
	else
		map_tab.get(object).setSelected(true);
}
/* [end] */



/* [start]	displays the data management module */
void showManager(String object){
	if(!map_tab.containsKey(object)){		
		Tabs tabs = stage.getTabs();
		Tab tab = new Tab(object);
		tab.setClosable(true);
		tab.setParent(tabs);
		tab.addEventListener("onClose", new CloseTabEventListener(object));
		
		Tabpanels tabpanels = stage.getTabpanels();
		Tabpanel tabpanel = new Tabpanel();
		tabpanel.setSclass("background_light_blue");
		tabpanel.setParent(tabpanels);
		stage.setSelectedPanel(tabpanel);
		
		map_tab.put(object, tab);
		
		if(object.equals(_courses)){
			Component win = Executions.createComponents("../../WEB-INF/macros/data_management/course_unit.zul", tabpanel, null);
		}
		if(object.equals(_sections)){
			Component win = Executions.createComponents("../../WEB-INF/macros/data_management/section_test.zul", tabpanel, null);
		}
		if(object.equals("Class List")){
			Component win = Executions.createComponents("../../WEB-INF/macros/data_management/class_list.zul", tabpanel, null);
		}
		
	}
	else
		map_tab.get(object).setSelected(true);
}
/* [end] */


void showCourseOffering(String object){
	if(!map_tab.containsKey(object)){		
		Tabs tabs = stage.getTabs();
		Tab tab = new Tab(object);
		tab.setClosable(true);
		tab.setParent(tabs);
		tab.addEventListener("onClose", new CloseTabEventListener(object));
		
		Tabpanels tabpanels = stage.getTabpanels();
		Tabpanel tabpanel = new Tabpanel();
		tabpanel.setSclass("background_light_blue");
		tabpanel.setParent(tabpanels);
		stage.setSelectedPanel(tabpanel);
		
		map_tab.put(object, tab);
		
		if(object.equals("Course Offering")){
			Component win = Executions.createComponents("../../WEB-INF/macros/registration/course_offering.zul", tabpanel, null);
		}
		
	}
	else
		map_tab.get(object).setSelected(true);
}


void signOut(){
	session.invalidate();
	Executions.sendRedirect("../../index.zul");
}
